<!--
@fucntion: 教改项目-申报
-->
<template>
  <el-row class="teachingreform-declaration">
    <el-col style="height:100%;">
      <table-panel v-show="!declarationDlgVisible" :name="'教学改革研究与实践项目-项目申报'" :columns="declarationColumns" :tableData="declarationData" :showRowNumber="false" :multiSelection="true"
      :canCreate="authorities.create" :canEdit="authorities.edit" :canDelete="authorities.delete" :customBtns="customBtns"
      :currentPage="declarationCurrentPage" :totalNum="declarationTotalNum" 
      @page-change="onPageChange('declaration',$event)" :pageSize="pageSize"
      @table-searching="onSearching('declaration',$event)"
      @table-create="onCreate('declaration',$event)" @table-edit="onEdit('declaration', $event)"
      @table-delete="onDelete('declaration',$event)" @table-export="onExport('declaration',$event)"
      @selection-change="onSelectionChange('declaration',$event)"
      @table-approval="onApproval"
      @table-multidelete="onMultiDel"></table-panel>
      <dec-form ref="decForm" v-show="declarationDlgVisible" @form-close="onCloseForm"
      :formRecord="declarationRecord"
      :formRules="declarationRules"></dec-form>
    </el-col>
  </el-row>
</template>
<script>
import TablePanel from "@/components/basic/TablePanel";
import {
  formModelInitFromTable,
  formRulesInitFromTable
} from "@/utils/formutils.js";
import { trueFalseRenderer,b,dateRenderer } from "@/utils/tableutils.js";
import Form from "./declaration/Form";

//meta info
var declaration = "declaration"; // 表格名称
var columnsAttr = "Columns"; // 列
var dataAttr = "Data"; // 数据
var selectionAttr = "Selection"; // 选中
var dlgVisibleAttr = "DlgVisible"; // 对话框
var formAttr = "Form";
var recordAttr = "Record"; // 表单模型
var rulesAttr = "Rules"; // 表单验证
var curPageAttr = "CurrentPage";// 分页-当前页
var totalNumAttr = "TotalNum";// 分页-总数
var qParamAttr = "QueryParams";// 查询参数

// 项目申报信息-列
var declarationColumns = [
  {
    prop: "xmsbbh",
    label: "项目申报编号",
    outOfSearch:true,
    hidden: true
  },
  {
    prop: "xmmc",
    label: "项目名称",
    rules:[{
      required: true,
      max: 50, 
      message: "必填，不超过50个字符",
      trigger: "blur"
    }]
  },
  {
    prop: "xmlx",
    label: "类型",
    rules:[{
      required: true,
      message: "请选择项目类型",
      trigger: "blur"
    }]
  },
  {
    prop: "jsbh",
    label: "项目主持人",
    outOfSearch:true,
    hidden:true
  },
  {
    prop: "xybh",
    label: "项目所在部门编号",
    outOfSearch:true,
    hidden:true
  },
  {
    prop: "xymc",
    label: "项目所在部门"
  },
  {
    prop: "sqjf",
    label: "申请经费",
    hidden:true
  },
  {
    prop: "yjkssj",
    label: "研究开始时间",
    type:"date",
    default:new Date(),
    renderer:dateRenderer,
    hidden:true
  },
  {
    prop: "yjjssj",
    label: "研究结束时间",
    type:"date",
    default:new Date(),
    renderer:dateRenderer,
    hidden:true
  },
  {
    prop:"xmsj",
    label:"项目时间",// 辅助字段
    type:"daterange",
    outOfSearch:true,
    hidden:true,
    rules:[{
      required: true,
      message: "请选择项目时间",
      trigger: "blur"
    }]

  },
  {
    prop: "tbsj",
    label: "填报时间",
    renderer:dateRenderer,
    type:"date"
  },
  {
    prop: "sqxxjf",
    label: "申请学校经费",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
  {
    prop: "ptxyjf",
    label: "配套学院经费",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
  {
    prop: "ptqtjf",
    label: "配套其他经费",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
  {
    prop: "zcjf",
    label: "自筹经费",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
  {
    prop: "xmszdwyj",
    label: "项目所在单位意见",
    hidden:true
  },
  {
    prop: "xmszdwtjrq",
    label: "项目所在单位推荐日期",
    type:"date",
    hidden:true
  },
  {
    prop: "zjpsyj",
    label: "专家评审意见",
    hidden:true
  },
  {
    prop: "zjpsrq",
    label: "专家评审日期",
    type:"date",
    hidden:true
  },
  {
    prop: "xxspyj",
    label: "学校审批意见",
    hidden:true
  },
  {
    prop: "xxsprq",
    label: "学校审批日期",
    hidden:true
  },
  {
    prop: "xmzt",
    label: "项目状态"
  },
  {
    prop: "xmsbsurl",
    label: "项目申报书URL",
    hidden:true
  }
];

export default {
  name:"Declaration",
  components: {
    "table-panel": TablePanel,
    "dec-form":Form
  },
  methods: {
    onSearching: function(tname, params) {
      var me = this;
      let url = "/jgxmsbSbjcxx";// /static/data/faculty.json /static/data/declaration.json

      if(url !== "") {
        me[tname+qParamAttr] = {};
        me[tname+qParamAttr] = params;
        if (params.isAccurate) {
          me[tname+qParamAttr] = params.paramPairs;
        } else {
          me[tname+qParamAttr].keywords = params.query;
        }
        console.log("参数=",me[tname+qParamAttr]);
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.results;
              me[tname+totalNumAttr] = resp.data.total;
            }
        });
      }
    },
    onCreate: function(tname) {
      // 新增
      var me = this;
      me[tname+recordAttr] = formModelInitFromTable(me[tname+columnsAttr]);
      me[tname+recordAttr].sfqy = "1";// 创建时，有效否是选中的

      me[tname+dlgVisibleAttr] = true;
      me.$refs.decForm.init();
    },
    onEdit: function(tname, row) {
      // 编辑
      var me = this;
      me[tname + recordAttr]= row;
      
      me[tname + dlgVisibleAttr] = true;
      me.$refs.decForm.init();
    },
    onDelete: function(tname, row) {
      // 删除
      var me = this;
      me.$confirm("确定删除这条记录吗?", "提示", {
        type: "danger"
      }).then(() => {
        me.deleteRequest(`/jgxmsbSbjcxx/${row.xmsbbh}`).then((resp)=>{
          if(resp.status == 200) {
            me.$message.success("删除成功!");
            me.onSearching(declaration,{});// 重新加载表格数据
          }else{
            me.$message.error("删除失败！");
          }
        });
      }).catch(() => {});
    },
    onExport: function(tname, params) {
      // 导出
      var me = this;
      me.$message.info("发送导出请求，返回下载路径");
    },
    onSelectionChange: function(tname, data) {
      // 选中改变
      var me = this;
      // 缓存记录，按照角色编号，查询对应的选中权限节点数组
      me[tname+selectionAttr] = data;
    },
    onConfirm: function(tname) {
      // 对话框-确认
      var me = this;
      // 验证信息
      var ret;
      me.$refs[tname + formAttr].validate((result, what)=>{
        ret = result;
      });
      if(!ret){
        me.$message.error("表单信息有误");
        return;
      }
      // 发送请求
      me.postRequest("/static/data/testSuc.json").then((resp)=>{
        if (resp.status == 200) {
          me.$message.success("提交成功！");
          //最后，隐藏对话框
          me[tname + dlgVisibleAttr] = false;
        } else {
          me.$message.error( "服务器发生错误，代码：" + resp.status);
        }
      });
    },
    onCloseForm:function(){// 关闭表单
      var me = this;
      me[declaration + dlgVisibleAttr] = false;
      me[declaration+recordAttr] = formModelInitFromTable(me[declaration+columnsAttr]);// 初始化form的model
      me.onSearching(declaration,{});// 重新加载表格数据
    },
    onApproval:function(){// 审批
      console.log("审批！");
    },
    onPageChange:function(tname,page){
      var me = this;
      let url = "/jgxmsbSbjcxx";

      if(url !== ""){
        me[tname+curPageAttr] = page;
        me[tname+qParamAttr]["page"] = page;
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.results;
              me[tname+totalNumAttr] = resp.data.total;
            }
        });
      }

    },
    onMultiDel:function(){
      let me = this;
      console.log("selected=",me[declaration + selectionAttr].length);

      me.$confirm("确定删除这些记录吗?", "提示", {
        type: "danger"
      }).then(() => {
        me[declaration + selectionAttr].forEach((item)=>{
          me.deleteRequest(`/jgxmsbSbjcxx/${item.xmsbbh}`).then((resp)=>{
            if(resp.status == 200) {
              me.$message.success("删除成功!");
              me.onSearching(declaration,{});// 重新加载表格数据
            }else{
              me.$message.error("删除失败！");
            }
          });
        });
        
      }).catch(() => {});

    }
  },
  data() {
    return {
      authorities:{
        create:true,
        delete:true,
        edit:true,
        approval:true,
        multiDelete:true
      },// 权限控制
      [declaration + columnsAttr]: declarationColumns, // 表格-列
      [declaration + dataAttr]: [], // 表格的数据
      [declaration + selectionAttr]: [], // 当前选中
      [declaration + dlgVisibleAttr]: false, // 对话框是否可见
      [declaration + recordAttr]: {}, // 表单模型
      [declaration + rulesAttr]: {}, // 表单验证
      [declaration + curPageAttr]:1,// 当前页
      [declaration + totalNumAttr]:0,// 总数
      [declaration + qParamAttr]:{},// 查询参数
      pageSize:20,// 每页数量
      customBtns:[]
    };
  },
  created: function() {
    var me = this;
    // 添加权限控制
    // me.authorities = me.$route.meta.authorities;
    if(me.authorities.approval){
      me.customBtns.push({
        type:"success",
        text:"审批",
        icon:"el-icon-check",
        eventName:"table-approval",
      });
    }
    if(me.authorities.multiDelete){
      me.customBtns.push({
        type:"danger",
        text:"删除多条",
        icon:"el-icon-check",
        eventName:"table-multidelete",
      });
    }

    // 表单模型初始化
    me[declaration+recordAttr] = formModelInitFromTable(me[declaration+columnsAttr]);
    // 表单验证初始化
    me[declaration+rulesAttr] = formRulesInitFromTable(me[declaration+columnsAttr]);

    //
    me.onSearching(declaration,{});
  }
}
</script>
<style lang="scss">

.teachingreform-declaration{
  height:100%;
}
</style>
